//
//  YYLSportTrack.m
//  YYLSport
//
//  Created by yangyinglei on 2017/4/22.
//  Copyright © 2017年 yangyinglei. All rights reserved.
//

#import "YYLSportTrack.h"


@implementation YYLSportTrack{

//记录每个折线的起点
CLLocation *_source;
}


-(instancetype)initWithSpotrType:(SportType)sportType withSportState:(YYLSportState)sportState{
    if (self = [super init]) {
        //记录运动类型
        _sportType = sportType;
        //设置运动图片类型
        NSString *sportTypeimgName;
        switch (sportType) {
            case SpotrTypeWalking:
                sportTypeimgName = @"map_annotation_walk";
                break;
            case SpotrTypeRunning:
                sportTypeimgName = @"map_annotation_run";
                break;
            case SpotrTypeRiding:
                sportTypeimgName = @"map_annotation_bike";
                break;
                
            default:
                break;
        }
        _sportTypeimgName = sportTypeimgName;
        _sportState = sportState;
    }
    return self;
}

- (YYLPolyLine *)appendPolylineWithDest:(CLLocation *)dest {
    //判断运动的状态   不等于继续的时候
    if (self.sportState != SportStateContinue) {
        //把起点掷空  下次继续画线就不会用这个原来的起点了
        _source = nil;
        return nil;
    }
    
    
    //速度小于等于0  返回   小于0是因为在室内会出现信号干扰  等于0是因为没运动
    if (dest.speed <= 0) {
        return nil;
    }
    //如果记录的时间过早 就返回
    if ([[NSDate date] timeIntervalSinceDate:dest.timestamp] > 2) {
        return nil;
    }
    //第一次行走调用
    if (_source == nil) {
        _source = dest;
        //生成大头针模型
        _startAnno = [[MAPointAnnotation alloc] init];
        _startAnno.coordinate = dest.coordinate;
    }
    YYLPolyLine *polyline = [YYLPolyLine PolylineWithSourceLocation:_source andDest:dest];
    
    _source = dest;
    return polyline;
}

@end
